/**
 * 项目名：pactera-boot
 * 包名：com.pactera.controller.sys.api
 * 文件名：SysRoleApi.java
 * 版本信息：1.0.0
 * 日期：2018年11月27日-上午11:03:31
 * Copyright (c) 2018 Pactera 版权所有
 */
package com.pactera.controller.sys.api;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pactera.common.constants.Constants;
import com.pactera.common.page.PageInfo;
import com.pactera.common.utils.JsonResponse;
import com.pactera.common.utils.JsonResponseUtils;
import com.pactera.controller.common.web.BaseController;
import com.pactera.model.SysRole;
import com.pactera.service.system.SysUserRoleService;
import com.pactera.vo.common.BaseRespVo;
import com.pactera.vo.common.PageRespVo;
import com.pactera.vo.role.RoleReqAddVo;
import com.pactera.vo.role.RoleReqBrowseVo;
import com.pactera.vo.role.RoleRespPageVo;
import com.pactera.vo.role.RoleRespVo;
import com.pactera.vo.role.RoleReqUpdVo;
import com.pactera.vo.role.RoleRespListVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "SysRoleApi-角色相关接口")
@RestController
@RequestMapping("/api/role")
public class SysRoleApi extends BaseController{
	
	@Autowired
	private SysUserRoleService sysUserRoleService;
	
	/**
	 * @Title: browseSysRole 
	 * @Description: 浏览系统角色信息
	 * @param sysRole
	 * @return JsonResponse
	 * @author LiuGuiChao
	 * @date 2019年1月7日 上午11:26:52 
	*/
	@ApiOperation(value = "mybatis-browseSysRole-浏览系统角色信息", notes = "mybatis-browseSysRole-浏览系统角色信息")
	@RequestMapping(value="/browseSysRole", method = RequestMethod.POST)
	public RoleRespPageVo browseSysRole(RoleReqBrowseVo roleReqVo,
			@RequestParam(value = "pageNo", defaultValue = Constants.PAGE_NUM, required=true) Integer pageNum,
	        @RequestParam(value = "pageSize", defaultValue = Constants.PAGE_SIZE, required=true) Integer pageSize) {
		RoleRespPageVo roleRespPageVo = new RoleRespPageVo();
		try {
			PageRespVo<RoleRespVo> pageInfo = sysUserRoleService.querySysRoleByPage(pageNum, pageSize, roleReqVo);
			roleRespPageVo = JsonResponseUtils.successData(RoleRespPageVo.class, pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return roleRespPageVo;
	}
	
	/**
	 * @Title: browseUserRole 
	 * @Description: 浏览指定用户角色信息
	 * @param userId
	 * @return JsonResponse
	 * @author LiuGuiChao
	 * @date 2019年1月10日 上午11:00:36 
	*/
	@ApiOperation(value = "mybatis-browseSysRole-浏览指定用户角色信息", notes = "mybatis-browseSysRole-浏览指定用户角色信息")
	@RequestMapping(value="/browseUserRole", method = RequestMethod.POST)
	public RoleRespListVo browseUserRole(
			@RequestParam(value = "userId", defaultValue = "", required=true) String userId) {
		List<RoleRespVo> userRoleGroup = sysUserRoleService.queryByUserId(userId);
		RoleRespListVo roleRespListVo = new RoleRespListVo();
		try {
			roleRespListVo = JsonResponseUtils.successData(RoleRespListVo.class, userRoleGroup);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return roleRespListVo;
	}
	
	/**
	 * @Title: addSysRole 
	 * @Description: 添加系统角色
	 * @param sysRole
	 * @return JsonResponse
	 * @author LiuGuiChao
	 * @date 2019年1月7日 上午10:50:13 
	*/
	@ApiOperation(value = "mybatis-addSysRole-新增系统角色", notes = "mybatis-addSysRole-新增系统角色")
	@RequestMapping(value="/addSysRole", method = RequestMethod.POST)
	public BaseRespVo addSysRole(RoleReqAddVo roleAddReqVo) {
		boolean isSuccess = false;
		BaseRespVo baseRespVo = new BaseRespVo();
		try {
			if(roleAddReqVo != null) {
				isSuccess = sysUserRoleService.saveSysRole(roleAddReqVo);
			}else {
				baseRespVo = JsonResponseUtils.failure(BaseRespVo.class, "未指定系统角色ID信息！");
			}
			if(isSuccess) {
				baseRespVo = JsonResponseUtils.success(BaseRespVo.class, "系统角色删除成功！");
			}else {
				baseRespVo = JsonResponseUtils.failure(BaseRespVo.class, "系统异常，请联系管理员");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return baseRespVo;
	}
	
	/**
	 * @Title: deleteSysRole 
	 * @Description: 删除系统角色信息
	 * @param roleId
	 * @return JsonResponse
	 * @author LiuGuiChao
	 * @date 2019年1月7日 上午11:07:41 
	*/
	@ApiOperation(value = "mybatis-deleteSysRole-删除系统角色", notes = "mybatis-deleteSysRole-删除系统角色")
	@RequestMapping(value="/deleteSysRole", method = RequestMethod.POST)
	public BaseRespVo deleteSysRole(
			@RequestParam(value = "roleId", defaultValue = "", required=true) String roleId) {
		boolean isSuccess = false;
		BaseRespVo baseRespVo = new BaseRespVo();
		try {
			if(StringUtils.isNotEmpty(roleId)) {
				isSuccess = sysUserRoleService.deleteSysRole(roleId);
			}else {
				baseRespVo = JsonResponseUtils.failure(BaseRespVo.class, "未指定系统角色ID信息！");
			}
			if(isSuccess) {
				baseRespVo = JsonResponseUtils.success(BaseRespVo.class, "系统角色删除成功！");
			}else {
				baseRespVo = JsonResponseUtils.failure(BaseRespVo.class, "系统异常，请联系管理员");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return baseRespVo;
	}
	
	/**
	 * @Title: updateSysRole 
	 * @Description: 更新系统角色
	 * @param sysRole
	 * @return JsonResponse
	 * @author LiuGuiChao
	 * @date 2019年1月10日 上午10:53:25 
	*/
	@ApiOperation(value = "mybatis-updateSysRole-修改系统角色", notes = "mybatis-updateSysRole-修改系统角色")
	@RequestMapping(value="/updateSysRole", method = RequestMethod.POST)
	public BaseRespVo updateSysRole(RoleReqUpdVo roleUpdReqVo) {
		boolean isSuccess = false;
		BaseRespVo baseRespVo = new BaseRespVo();
		try {
			if(roleUpdReqVo != null) {
				isSuccess = sysUserRoleService.updateSysRole(roleUpdReqVo);
			}else {
				baseRespVo = JsonResponseUtils.failure(BaseRespVo.class, "未指定系统角色ID信息！");
			}
			if(isSuccess) {
				baseRespVo = JsonResponseUtils.success(BaseRespVo.class, "系统角色删除成功！");
			}else {
				baseRespVo = JsonResponseUtils.failure(BaseRespVo.class, "系统异常，请联系管理员");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return baseRespVo;
	}
}
